/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CategoryBean;
import ejb.CategoryBeanLocal;
import ejb.ItemBean;
import ejb.ItemBeanLocal;
import entities.Category;
import entities.Item;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class CategoryManagedBean {

    private List<Item> listItem;
    private String titleCategory;
    @EJB
    private CategoryBeanLocal categoryBean;
    @EJB
    private ItemBeanLocal itemBean;

    public CategoryManagedBean() {
        categoryBean = new CategoryBean();
        itemBean = new ItemBean();

    }

    public String initTitleCategory() {
        Category category = new Category();
        String nextView = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.NEXT_VIEW);
        String idCategory = nextView.split(Constants.SEPARATOR)[1];
        category.setIdCategory(Integer.valueOf(idCategory));
        category = categoryBean.findByIdCategory(category.getIdCategory());
        return category.getName();
    }

    public List<Item> initListItem() {
        String nextView = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.NEXT_VIEW);
        String idCategory = nextView.split(Constants.SEPARATOR)[1];
        Category cat = new Category();
        cat.setIdCategory(Integer.valueOf(idCategory));
        return itemBean.findByIdCategory(cat);
    }

    public CategoryBeanLocal getCategoryBean() {
        return categoryBean;
    }

    public void setCategoryBean(CategoryBeanLocal categoryBean) {
        this.categoryBean = categoryBean;
    }

    public List<Item> getListItem() {

        listItem = initListItem();

        return listItem;
    }

    public void setListItem(List<Item> listItem) {
        this.listItem = listItem;
    }

    public ItemBeanLocal getItemBean() {
        return itemBean;
    }

    public void setItemBean(ItemBeanLocal itemBean) {
        this.itemBean = itemBean;
    }

    public String getTitleCategory() {
        titleCategory = initTitleCategory();
        return titleCategory;
    }

    public void setTitleCategory(String titleCategory) {
        this.titleCategory = titleCategory;
    }

    public List<Category> parentsCategories() {
        return categoryBean.findChildrenForParent();
    }
    
    public List<Category> childrenFor(Category parent){
        return categoryBean.findByIdParentCategory(parent);
    }

}
